package com.jframe.sys.controller.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.support.rowset.SqlRowSet;

import com.jframe.app.model.DataSourse;
import com.jframe.app.service.DataSourseService;
import com.jframe.cfg.model.Tablemapping;
import com.jframe.cfg.service.TablemapService;
import com.jframe.sys.orm.dao.impl.JdbcTemp;
import com.jframe.sys.orm.jdbc.JdbcProperties;
import com.jframe.sys.utils.CacheUtil;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.SpringUtil;
import com.jframe.sys.utils.StringUtil;

public class TabmCache {
	
	public static Object getObject(String key){
		CacheUtil cache = CacheUtil.getInstance("dictionarydata");
		return cache.get(key);
	}
	
	/**
	 *  获取easyuicombobox的json
	 * @param key
	 * @return
	 */
	public static String getComboboxJson(String key){
		String json = (String)getObject(key);
		if(StringUtil.null2Space(json).equals("")){
			try{
				reload(key);
				json = (String)getObject(key);
			}catch(Exception e){
				json="";
			}
		}
		return json;
	}
	
	/**
	 * 
	 * @param key
	 * @param isReload 是否直接获取数据库总的表映射字典。
	 * @return
	 */
	public static String getComboboxJson(String key,boolean isReload){
		String json="";
		if(isReload){
			try{
				reload(key);
				json = (String)getObject(key);
			}catch(Exception e){
				json="";
			}
		}else{
			json=getComboboxJson(key);
		}
		return json;
	}
	
	/**
	 *  获取easyuicombotree的json
	 * @param key
	 * @return
	 */
	public static String getCombotreeJson(String key){
		String json = (String)getObject(key+"_tree");
		if(StringUtil.null2Space(json).equals("")||StringUtil.null2Space(json).equals("[]")){
			try{
				reload(key);
				json = (String)getObject(key+"_tree");
			}catch(Exception e){
				e.printStackTrace();
				json="";
			}
		}
		return json;
	}
	
	/**
	 * 
	 * @param key
	 * @param isReload 是否直接获取数据库总的表映射字典。
	 * @return
	 */
	public static String getCombotreeJson(String key,boolean isReload){
		String json ="";
		if(isReload){
			try{
				reload(key);
				json = (String)getObject(key+"_tree");
			}catch(Exception e){
				json="";
			}
		}else{
			json = getCombotreeJson(key);
		} 
		return json;
	}
	
	/**
	 * 获取easyuicombogrid的json
	 * @param key
	 * @return
	 */
	public static String getComboGridJson(String key){
		return getComboboxJson(key);
	}
	
	/**
	 * 
	 * @param key
	 * @param isReload 是否直接获取数据库总的表映射字典。
	 * @return
	 */
	public static String getComboGridJson(String key,boolean isReload){
		return getComboboxJson(key,isReload);
	}
	 
	public static void reload(String key){
		TablemapService  tablemapService = (TablemapService)SpringUtil.getBean("tablemapService");
		Tablemapping tablemap = tablemapService.load("enname",key);
		JdbcTemp jdbcTemp = new JdbcTemp();
		CacheUtil cache = CacheUtil.getInstance("dictionarycache");	
		if(StringUtil.null2Space(tablemap.getUpname()).equals("")){
			String sql = "select * from "+tablemap.getTablename()+" where 1=1 ";
			if(!StringUtil.null2Space(tablemap.getConditions()).equals("")){
				sql +="and "+tablemap.getConditions();
			}
			if(!StringUtil.null2Space(tablemap.getSeqname()).equals("")){
				sql +="  order by "+tablemap.getSeqname();
			}
			if(!StringUtil.null2Space(tablemap.getSorttype()).equals("")){
				sql +=" "+tablemap.getSorttype();
			}
			List<Map<String,Object>> list = jdbcTemp.queryForList(getConName(tablemap), sql);
			String json = createcomboboxjson(list,tablemap);
			cache.put(tablemap.getEnname(),json);
			cache.put(tablemap.getEnname()+"_tree",json);		
		}else{
			List<Map<String,Object>> list = findchildNode(jdbcTemp, tablemap, "");
			cache.put(tablemap.getEnname(), createcomboboxjson(list,tablemap));		
			list =  findchildNode(jdbcTemp,tablemap,"0");
			cache.put(tablemap.getEnname()+"_tree", createcombotreejson(list,jdbcTemp,tablemap));
		}
	}
	
	public static String createcombotreejson(List<Map<String,Object>> list,JdbcTemp jdbcTemp,Tablemapping tablemap){
		String json = "[";
		for(Map<String,Object> map:list){
			json +="{\"id\":\""+map.get(tablemap.getKeyname())+"\",";
			json +="\"text\":\""+map.get(tablemap.getValuename())+"\"";
			List<Map<String,Object>> childlist = findchildNode(jdbcTemp,tablemap,""+map.get("id"));
			String jsonstr = createcombotreejson(childlist,jdbcTemp,tablemap);
			if(jsonstr.length()>2){
				json +=",\"children\":"+jsonstr;
			}
			json +="},";
		}
		if(json.length()>2){
			json = json.substring(0,json.length()-1);
		}
		json +="]";	
		return json;
	}
	
	private static List<Map<String,Object>> findchildNode(JdbcTemp jdbcTemp,Tablemapping tablemap,String upId){
		if(!StringUtil.null2Space(tablemap.getUpname()).equals("")){		
			String sql = "select * from "+tablemap.getTablename()+" where 1=1";
			if(!StringUtil.null2Space(tablemap.getConditions()).equals("")){
				sql +=" and "+tablemap.getConditions();
			}
			if(!StringUtil.null2Space(upId).equals("")){
				String upvalue = upId;			
				sql +=" and "+tablemap.getUpname()+"="+upvalue;
			}
			if(!StringUtil.null2Space(tablemap.getSeqname()).equals("")){
				sql +=" order by "+tablemap.getSeqname();
			}
			if(!StringUtil.null2Space(tablemap.getSorttype()).equals("")){
				sql +=" "+tablemap.getSorttype();
			}
			return jdbcTemp.queryForList(getConName(tablemap),sql);
		}else{
			return new ArrayList<Map<String,Object>>();
		}
	}
	
	private static String createcomboboxjson(List<Map<String,Object>> list,Tablemapping tablemap){
		String json = "[";
		for(Map<String,Object> map:list){
			json +="{\"id\":\""+map.get(tablemap.getKeyname())+"\",";
			json +="\"text\":\""+map.get(tablemap.getValuename())+"\"";
			json +="},";
		}
		if(json.length()>2){
			json = json.substring(0,json.length()-1);
		}
		json +="]";	
		return json;
	}
	
	private static String getConName(Tablemapping tablemapping){		
		DataSourseService dataSourseService = (DataSourseService)SpringUtil.getBean("dataSourseService");
		List<DataSourse> dataSoursels = dataSourseService.findBySql(DataSourse.class, "select * from t_app_datasourse where appId = "+tablemapping.getTypeId());
		DataSourse dataSourse = dataSoursels.get(0);
		return dataSourse.getConName();		
	}	
	
} 
